package cn.yhbk.web.modules.manager.Service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.modules.manager.Pojo.Entity.OrderResult;
import cn.yhbk.web.modules.manager.Mapper.OrderResultMapper;
import cn.yhbk.web.modules.manager.Pojo.Entity.OrderResultItem;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbClass;
import cn.yhbk.web.modules.manager.Pojo.VO.OrderResult.*;
import cn.yhbk.web.modules.manager.Service.IOrderResultItemService;
import cn.yhbk.web.modules.manager.Service.IOrderResultService;
import cn.yhbk.web.modules.manager.Service.ITbClassService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class OrderResultServiceImpl extends ServiceImpl<OrderResultMapper, OrderResult> implements IOrderResultService {

    @Autowired
    private IOrderResultItemService itemService;

    @Autowired
    private ITbClassService classService;

    @Override
    public Map<String, Object> getOrderResult(Long currentPage, Long pageSize, OrderResultParam condition) {
        Page<OrderResult> page = new Page<>(currentPage, pageSize);

        LambdaQueryWrapper<OrderResult> lambda = new QueryWrapper<OrderResult>().lambda();

        Long id = condition.getId();
        lambda.eq(!Objects.isNull(id), OrderResult::getId, id);

        String name = condition.getName();
        lambda.like(!Strings.isBlank(name), OrderResult::getName, name);

        Page<OrderResult> resPage = page(page, lambda);

        List<OrderResultRes> resList = myPackage(resPage.getRecords());
        System.out.println(resList.size());

        return MyUtil.makeRespPage(resPage, resList);
    }

    private List<OrderResultRes> myPackage(List<OrderResult> records) {


        List<OrderResultClassIdRes> classIdResList =  itemService.packageResult(records);
        List<Long> classIds = classIdResList.stream()
                .flatMap(item -> item.getClassIdList().stream().map(OrderResultItem::getClassId))
                .filter(item -> !Objects.isNull(item))
                .distinct()
                .collect(Collectors.toList());

        List<TbClass> classList;

        if (classIds.isEmpty())classList=new ArrayList<>();
        else classList= classService.listByIds(classIds);

        Map<Long, TbClass> classMap = classList.stream().collect(Collectors.toMap(TbClass::getId, item -> item));


        return classIdResList.stream().map(item->{
            System.out.println(1);
            OrderResultRes resTmp = new OrderResultRes();
            BeanUtil.copyProperties(item,resTmp);
            List<ItemResponse> classListTmp = item.getClassIdList().stream()
                    .map(item2->{
                        ItemResponse itemResponse = new ItemResponse();
                        TbClass tbClass = classMap.get(item2.getClassId());
                        BeanUtil.copyProperties(tbClass,itemResponse);
                        itemResponse.setItemId(item2.getId());
                        return itemResponse;
                    })
                    .collect(Collectors.toList());
            resTmp.setClassList(classListTmp);

            return resTmp;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean orderResultModify(Long orderResultId, OrderResultModify modify) {
        if (Objects.isNull(orderResultId)) return false;
        LambdaUpdateWrapper<OrderResult> lambda = new UpdateWrapper<OrderResult>().lambda();

        lambda.eq(OrderResult::getId, orderResultId);

        String name = modify.getName();
        lambda.set(!Strings.isBlank(name), OrderResult::getName, name);

        return update(lambda);
    }


}
